<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sw">
              <a-switch v-model="form.sw"></a-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrl">
              <j-image-upload v-model="form.imgUrl"></j-image-upload>
              <div>(文案调整：建议尺寸 300pt*400pt)</div>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="显示秒数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seconds">
              <a-input-number v-model="form.seconds" placeholder="请输入显示秒数" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="点击效果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-select v-model="form.type" sstyle="width: 100%" @change="selectLinkTyle($event)" placeholder="请选择点击效果">
                <a-select-option v-for="item in link_types" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="点击效果对应参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="param">
              <!-- <a-input v-model="form.param" placeholder="请输入点击效果对应参数"></a-input> -->
              <gospel-select :width="'100%'" :size="'default'" :link_type="link_type" v-model="form.param">
              </gospel-select>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import baseData from '@/utils/base-data.js'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JDictSelectTag from "@/components/dict/JDictSelectTag"
import JSwitch from '@/components/jeecg/JSwitch'
import GospelSelect from '@/components/gospel/GospelSelect'

export default {
  name: 'CmsIndexPopupForm',
  components: {
    JFormContainer,
    JDate,
    JImageUpload,
    JDictSelectTag,
    JSwitch,
    GospelSelect
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      link_type: {
        id: 0,
        name: '点击无效果',
        params_type: 0 //0:无参数，1:输入参数，2:选择参数
      },
      link_types: baseData.link_types,
      form: {
        sw: false,
        imgUrl: '',
        seconds: 0,
        type: 0,
        param: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        sw: [
          { required: true, message: '请选择是否启用' },
        ],
        imgUrl: [
          { required: true, message: '请选择图片!' },
        ],
        // seconds: [
        //   { required: true, message: '请输入显示秒数!' },
        // ],
        type: [
          { required: true, message: '请选择点击效果类型' },
        ]
      },
      url: {
        add: "/common/cmsIndexPopup/add",
        edit: "/common/cmsIndexPopup/edit",
        queryById: "/common/cmsIndexPopup/queryById"
      }
    }
  },
  computed: {
    formDisabled () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    selectLinkTyle (e) {
      for (let i = 0; i < this.link_types.length; i++) {
        if (e == this.link_types[i].id) {
          this.link_type = this.link_types[i]
        }
      }
      this.ruleForm.param = ""
    },
    add () {
      this.edit({
        sw: false,
        imgUrl: '',
        seconds: 0,
        type: 0,
        param: ''
      })
    },
    edit (record) {
      this.form = Object.assign({}, record)
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData () {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm () {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.form.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback (row) {
      this.form = row
    },
  }
}
</script>
